package org.alex.netShop.pages;

import java.util.Collection;



import org.alex.netShop.beans.PurchaseBean;
import org.alex.netShop.interfaces.Customer;
import org.alex.netShop.interfaces.Customers;
import org.alex.netShop.interfaces.Product;
import org.alex.netShop.interfaces.Products;
import org.alex.netShop.interfaces.Purchase;
import org.alex.netShop.interfaces.Seller;
import org.alex.netShop.interfaces.Sellers;
import org.alex.netShop.service.Purchases;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

public class AddPurchase
{
	@Persist
	private String purchaseSeller;
	@Persist
	private String purchaseCustomer;
	@Persist
	private String purchaseProduct;
	@InjectPage
	private ViewPurchase viewPurchasePage;
	@InjectPage
	private ViewError viewErrorPage;

	@Inject
	private Products products;
	@Inject
	private Sellers sellers;
	@Inject
	private Customers customers;
	@Property
	@Inject
	private Purchases purchases;

	public Collection<Product> getProducts() {
		return products.retrieve();
	}
	public Collection<Seller> getSellers() {
		return sellers.retrieve();
	}
	public Collection<Customer> getCustomers() {
		return customers.retrieve();
	}
	@OnEvent(value = "submit", component = "purchaseInputForm")
	Object onFromSubmit() {

		Product product = null;
		Customer customer = null;
		Seller seller = null;
		try {
			
			customer = customers.retrieveCustomer(getPurchaseCustomer());
//			reader = readers.retrieveSingleReader(getBorrowReader());
			seller = sellers.retrieveSeller(getPurchaseSeller());
//			librarian = librarians.retrieveSingleLibrarian(getBorrowLibrarian());
			product = products.retrieveProduct(getPurchaseProduct());
//			book = books.retrieveSingleBook(getBorrowBook());
		} catch (Exception ex) {
			viewErrorPage.setPassedMessage(ex.toString());
			return viewErrorPage;
		}
		if (customer == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Customer u Map-i");
			return viewErrorPage;
		}
		if (seller == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Seller u Map-i");
			return viewErrorPage;
		}
		if (product == null) {
			viewErrorPage
					.setPassedMessage("Greska u prepoznavanju objekta Product u Map-i");
			return viewErrorPage;
		}
		Purchase purchaseBean = new PurchaseBean();

		purchaseBean.executeBorrowCreation(seller, customer, product, purchases);
//		borrowBean.executeBorrowCreation(reader, librarian, book, borrows);

//		this.borrowBook = "";
		this.purchaseProduct = "";
//		this.borrowLibrarian = "";
		this.purchaseCustomer = "";
//		this.borrowReader = "";
		this.purchaseSeller = "";
		
		return viewPurchasePage;
	}

	
	public String getPurchaseSeller() {
		return purchaseSeller;
	}
	public void setPurchaseSeller(String purchaseSeller) {
		this.purchaseSeller = purchaseSeller;
	}
	public String getPurchaseCustomer() {
		return purchaseCustomer;
	}
	public void setPurchaseCustomer(String purchaseCustomer) {
		this.purchaseCustomer = purchaseCustomer;
	}
	public String getPurchaseProduct() {
		return purchaseProduct;
	}
	public void setPurchaseProduct(String purchaseProduct) {
		this.purchaseProduct = purchaseProduct;
	}

}
